home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / media / objects / converte / obj2ray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-03  |  4.1 KB  |  192 lines

  1. /* A minimal Wavefront to Rayshade converter */
  2. /* specifically, the minimum necessary to import Viewpoint files */
  3. /* written because I got sick of fighting with obj2nff.awk & nff2shade.awk */
  4.  
  5. /* cc obj2ray.c -o obj2ray
  6. /* Usage: obj2ray <input_file.obj> > <output_file.ray>
  7.  
  8. /* Jeff Katcher, katcher@netcom.com */
  9. /* 9/2/93 */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #define LINELEN 255
  15. #define NEWLINE '\n'
  16.  
  17. typedef struct {
  18.    float x,y,z;
  19. } vertex;
  20.  
  21. vertex *vertices;
  22. int vertice_count;
  23.  
  24. vertex *vnormals;
  25. int normal_count;
  26.  
  27. void strip_newline(line)
  28. char *line;
  29. {
  30.    char *s;
  31.  
  32.    if (s=strrchr(line,NEWLINE))
  33.       *s=NULL;
  34. }
  35.  
  36. int add_vertex(line)
  37. char *line;
  38. {
  39.    if (!vertice_count)
  40.       vertices=(vertex *)malloc(sizeof(vertex));
  41.    else
  42.       vertices=(vertex *)realloc(vertices,sizeof(vertex)*(vertice_count+1));
  43.  
  44.    sscanf(line,"v %f %f %f",&(vertices[vertice_count].x),
  45.       &(vertices[vertice_count].y),&(vertices[vertice_count].z));
  46.  
  47.    return(++vertice_count);
  48. }
  49.  
  50. int add_vertex_normal(line)
  51. char *line;
  52. {
  53.    if (!normal_count)
  54.       vnormals=(vertex *)malloc(sizeof(vertex));
  55.    else
  56.       vnormals=(vertex *)realloc(vnormals,sizeof(vertex)*(normal_count+1));
  57.  
  58.    sscanf(line,"vn %f %f %f",&(vnormals[normal_count].x),
  59.       &(vnormals[normal_count].y),&(vnormals[normal_count].z));
  60.  
  61.    return(++normal_count);
  62. }
  63.  
  64. int count_sides(line)
  65. char *line;
  66. {
  67.    char *s;
  68.    int count=0;
  69.  
  70.    s=strdup(line);
  71.  
  72.    strtok(s," "); /* skip 'f' type identifier */
  73.    while (strtok(NULL," "))
  74.       count++;
  75.  
  76.    free(s);
  77.  
  78.    return(count);
  79. }
  80.  
  81. void parse_facet(line)
  82. char *line;
  83. {
  84.    char *token;
  85.    char *split;
  86.    int vertice;
  87.    int normal;
  88.    int sides;
  89.  
  90.    sides=count_sides(line);
  91.  
  92.    if (sides==3)
  93.       printf("triangle\n");
  94.    else
  95.       printf("poly\n");
  96.  
  97.    strtok(line," "); /* skip 'f' type identifier */
  98.    while (token=strtok(NULL," "))
  99.       {
  100.       vertice=atoi(token)-1;
  101.       printf("%f %f %f",vertices[vertice].x,
  102.          vertices[vertice].y,vertices[vertice].z);
  103.  
  104.       if (sides==3 && (split=strstr(token,"//")))
  105.          {
  106.          normal=atoi(split+2)-1;
  107.          printf(" %f %f %f",vnormals[normal].x,vnormals[normal].y,
  108.             vnormals[normal].z);
  109.          }
  110.  
  111.       printf("\n");
  112.       }
  113. }
  114.  
  115. void convert_comment(line)
  116. char *line;
  117. {
  118.    char *s;
  119.  
  120.    printf("/* %s */\n",(s=strchr(line," "))?s+1:line+1);
  121. }
  122.  
  123. main(argc,argv)
  124. int argc;
  125. char **argv;
  126. {
  127.    FILE *fp;
  128.    char line[LINELEN+1];
  129.    char *s;
  130.  
  131.    if (argc!=2)
  132.       fprintf(stderr,"obj2ray: obj2ray <input.obj>\n");
  133.    else
  134.       if (!(fp=fopen(argv[1],"r")))
  135.          fprintf(stderr,"obj2ray: could not open '%s' for input\n",argv[1]);
  136.       else
  137.          {
  138.          while (fgets(line,LINELEN,fp))
  139.             {
  140.             strip_newline(line);
  141.  
  142.             switch (*line)
  143.                {
  144.                case '#': /* comment */
  145.                case '$': /* seems to be comment */
  146.                   convert_comment(line);
  147.                   break;
  148.  
  149.                case 'v': /* vertex */
  150.                   switch (*(line+1))
  151.                      {
  152.                      case ' ': /* regular vertex 'v' */
  153.                         add_vertex(line);
  154.                         break;
  155.  
  156.                      case 'n': /* vertex normal 'vn' */
  157.                         add_vertex_normal(line);
  158.                         break;
  159.  
  160.                      default: /* unsupported vertex type */
  161.                         fprintf(stderr,"Unsupported vertex type: '%s'\n",line);
  162.                      }
  163.                   break;
  164.  
  165.                case 'g': /* group */
  166.                   convert_comment(line);
  167.                   break;
  168.  
  169.                case 's': /* smoothing group # */
  170.                   /* convert_comment(line); */
  171.                   break;
  172.  
  173.                case 'f': /* facet */
  174.                   parse_facet(line);
  175.                   break;
  176.  
  177.                case NULL: /* Empty line */
  178.                   printf("\n");
  179.                   break;
  180.  
  181.                default:
  182.                   fprintf(stderr,"Error: cannot handle: '%s'\n",line);
  183.                }
  184.             }
  185.  
  186.          fclose(fp);
  187.  
  188.          printf("/* obj2ray: created %d vertices */\n",vertice_count);
  189.          printf("/* obj2ray: created %d vertex normals */\n",normal_count);
  190.          }
  191. }
  192.